<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class MessageController extends AdminController{
	//留言主页面
	public function index(){
		$where = '1=1';
		$where .= I('get.cid') > 0 ? ' and cid=' . I('get.cid', 0, 'intval') : '';
		$where .= I('get.keyWord') ? ' and (name like "%' . I('get.keyWord') . '%" or msg like "%' . I('get.keyWord') . '%")' : '';
		$where .= I('get.status', -1) > -1 ? ' and status =' . I('get.status') : '';
		$where .= I('get.start_time') ? ' and times>=' . strtotime(I('get.start_time')) : '';
		$where .= I('get.end_time') ? ' and times<' . (strtotime(I('get.end_time')) + 24*3600) : '';

		//BaseController 中的getList方法很重要
		$list = $this->getList('*', 'message', $where, 'id desc', true);
		$this->assign('list', $list);
		
		$classifyList = $this->getList('id,name', 'message_classify', null, 'sort desc');
		$this->assign('classifyList', $classifyList);
		$this->display();
	}

	//取单独的信息数据，并且以Json格式返回，数据内容存于：info
	public function getMsgInfo(){
		$info = $this->getInfo('*', 'message', array('id' => I('get.id', 0, 'intval')));
		if($info){
			$info['times'] = $info['times']?date("Y-m-d H:i",$info['times']):"";
			$info['reply_times'] = $info['reply_times']?date("Y-m-d H:i",$info['reply_times']):date("Y-m-d H:i");
			if($info['cid']){
				$_cid_info = $this->getInfo('*', 'message_classify', array('id' => $info['cid']));
				$info['c_name']	= $_cid_info['name'];
			}
			$data['info'] = $info;
			$data['status'] = 1;
		}else{
			$data['info'] = '数据不存在';
			$data['status'] = 0;
		}
		$this->ajaxReturn($data);
	}
	
	//回复
	public function reply(){
		if(IS_POST){
			$_POST['times'] = $_POST['times']?strtotime($_POST['times']):"";
			$_POST['reply_times'] = strtotime('now');
			$result = $this->updateData($_POST, 'message', 2);
			$this->returnResult($result);
		}	
	}


	//删除
	public function del(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'message');
		$this->returnResult($result);
	}

   //分类主页面
	public function classify(){
		$list = $this->getList('*', 'message_classify', null, 'sort desc,id asc',ture);
		$this->assign('list', $list);
		$this->display();
	}
	//分类添加
	 public function addClassify(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'message_classify');
			$this->returnResult($result, null, U('Message/classify'));
		}
	}
	//分类修改
	public function editClassify(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'message_classify', 2);
			$this->returnResult($result, null, U('Message/classify'));
		}else{
			$info = $this->getInfo('*', 'message_classify', array('id' => I('get.id', 0, 'intval')));
			$this->ajaxReturn($info);
		}
	}
	//分类删除
	public function delClassify(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'message_classify');
		$this->returnResult($result);
	}
}